    #include "define.h"   
    #include "variable.h"   
    #include "I2C.h"   
    #include "osddisplay.h"   
    #include "key.h"   
    #include "IEBus.h"   
    #include "process.h"   
    #include "memory.h"   
       
    void UART_Init()//UART 初始化   
    {    
       UBRRL=25;//25;//51;    
       UCSRB=0x18;//RXEN=1;TXEN=1;   
       UCSRA=0x00;   
    }   
       
    void Adc_Init(void)//Test OK!   
    {   
       ADMUX=ADC_REFS+ADC_ADLAR+ADC_MUX;//参考电压，右对齐，通道   
       ADCSRA=ADC_ADPS;//分频系数      
    }   
       
    void Time1_Init(void)   
    {   
       //TOIE1_Enable;   
    }   
       
    void Send_Char(uchar c)//串口通信   
    {   
      ClrBit(UCSRA,TXC);   
      UDR=c;   
      while(ValBit(UCSRA,TXC)==0);   
      ClrBit(UCSRA,TXC);   
      NmDelay(1);   
    }   
       
    void Io_Init(void)   
    {   
       TIMEDDR=0xFF;   
       IEBusDDR=0xFF;   
       I2CDDR=0xFF;   
          
       ClrBit(IEBusDDR,2);   
       ClrBit(IEBusPORT,2);   
          
       SetBit(PORTD,3);   
       ClrBit(DDRD,3);   
          
       MCUCR=0x0A;//INT0    
    }   
       
    void PowerOn_Init(void)   
    {   
       Io_Init();   
       DECODE_High;   
       I2c_Init();    
       RTC_Init();   
       UART_Init();   
       Adc_Init();   
       ADC_Enable;   
       ADIE_Enable;   
       INT0_Enable;   
       TOIE1_Enable;   
       TCCR1A=0x00;   
    }   
    void Sys_Init(void)   
    {   
          
    }   
       
    /* REMO COMMAND */   
    #pragma vector=INT0_vect   
    __interrupt void Int0(void)//定义成上升沿触发,定时器采用time0,8位定时器，clk=8M,1024分频，定时最长时间为32ms   
    {   
      uint temp;   
      TCCR1B=CLK_STOP;   
      temp=TCNT1;   
      TCNT1=0;    
      if(RedRead==0)   
        {   
          RedRead=1;   
          RedCount=0;   
          RedData[0]=0;   
          RedData[1]=0;   
          //RedData[2]=0;   
          RedData[3]=0;         
          TCCR1B=CLK1;//start time0 clk/1024   
          return ;   
         }   
      else if(RedRead==1)//4.5ms   
       {          
         if(temp>Time1_4_5_Small&&temp<Time1_4_5_Big)//4.5ms   
           {     
             RedRead=2;            
             TCCR1B=CLK1;   
            }    
         else if(temp<Time1_2_2_Big&&temp>Time1_2_2_Small)//在识别到按键主值后,长按时波形会是2.2ms，两次2.2ms会在这里停留，从而进行累加，从而判断出长按   
             {     
                   RedCnt++;   
                   if(RedCnt>5&&RedCmd!=0xFF)   
                     {   
                       INT0_Disable;   
                       RedSuccess=1;   
                       RedCmd=RedData[2];   
                       RedLong=1;   
                       RedCnt=0;   
                       return;   
                      }   
                   TCCR1B=CLK_64;   
                   TCNT1=0xC000;//等待100ms后，在溢出中断里面执行     
                   RedRead=0;                
             }                     
         else//error   
           {          
             RedRead=0;    
            }   
         return ;   
        }   
      else if(RedRead>=2&&RedRead<=5)//data   
       {   
         RedCount++;   
         RedData[RedRead-2]>>=1;//LSB is first   
         if(temp> Time1_1_7_Small&&temp<Time1_1_7_Big)//1.7ms---> 1   
         {   
           RedData[RedRead-2]|=0x80;   
         }   
         else if(temp> Time1_0_6_Small&&temp<Time1_0_6_Big)//0.6ms ----> 0   
         {   
           RedData[RedRead-2]&=0x7F;   
         }   
         else//error return   
         {   
           RedRead=0;   
         }   
         if(RedCount>=8)//receive a byte   
           {   
             //Send_Char(RedData[RedRead-2]);//test the receiving data   
             RedRead++;//receive next byte   
             RedCount=0;   
            }   
         if(RedRead==6)//receive complete   
           {   
             if((RedData[0]==REDCUSTOM0)&&(RedData[1]==REDCUSTOM1)   
                /*&&(RedData[2]==~RedData[3])*/) //receive is ok!   
                {   
                   RedCmd=RedData[2];    
                      
                   if(RedCmd==REMOONE||RedCmd==REMOTWO||RedCmd==REMOTHREE||RedCmd==REMOFOUR||   
                      RedCmd==REMOFIVE||RedCmd==REMOSIX||RedCmd==REMOSEVEN||RedCmd==REMOEIGHT)   
                      {   
                        TCCR1B=CLK_64;   
                        TCNT1=0xC000;//等待100ms后，在溢出中断里面执行     
                       }     
                   else   
                      {   
                        INT0_Disable;                  
                        RedSuccess=1;   
                      }                           
                   RedCnt=0;   
                   RedRead=0;   
                   RedCount=0;          
                }    
              else    
                 RedRead=0;    
             }   
         else if(RedRead>=2&&RedRead<=5)//receice continue   
               TCCR1B=CLK1; //start time0;   
       }      
    }   
       
       
    #pragma vector=TIMER1_OVF_vect   
    __interrupt void Time1_Over_Interrupt(void)   
    {   
          if(RedCmd!=0xFF)   
          {   
            RedSuccess=1;   
            RedCnt=0;   
            INT0_Disable;   
          }   
          Send_Char(0x13);   
          RedLong=0;   
          RedCnt=0;   
          RedRead=0;   
          RedData[0]=0;   
          RedData[1]=0;   
          //RedData[2]=0xFF;   
          RedData[3]=0;      
          TCCR1B=CLK_STOP;   
          TCNT1=0;         
    }   
       
    #pragma vector=INT1_vect   
    __interrupt void Int1(void)//   
    {   
       RTC_SendData(0x01,0x11);   
       RTC_SendData(0x0F,64);   
       Hour=RTC_ReadData(0x04)&0x3F;   
       Minute=RTC_ReadData(0x03)&0x7F;   
       Year=RTC_ReadData(0x08);   
       Month=RTC_ReadData(0x07)&0x1F;   
       Data=RTC_ReadData(0x05)&0x3F;   
          
       SetHour=(Hour/16*10)+Hour%16;   
       SetMinute=(Minute/16*10)+Minute%16;   
       SetYear=(Year/16*10)+Year%16;   
       SetMonth=(Month/16*10)+Month%16;   
       SetData=(Data/16*10)+Data%16;   
          
       if(UsrMenu&0x02)   
          Time_Disp(Hour,Minute);   
       else   
          Row_Clear(STATUSROW,23,6);   
       if(UsrMenu&0x01)   
          Data_Disp(Year,Month,Data);   
       else   
          Row_Clear(STATUSROW+1,20,9);    
    }   
       
    /*  
    #pragma vector=TIMER0_OVF_vect  
    __interrupt void Time0_Over_Interrupt(void)  
    {  
        
    }  
    */   
       
       
    uchar Key_Judge(uchar c)// AD0 or AD1 key judge   
    {   
       if(c>0xD7)//0x35C,4.2V----> No key press   
           return NOKEY;   
       else if(c>0xB8)//0x2E0,3.6V --->3.8Vkey   
           return KEY01;   
       else if(c>0x9E)//0x27A,3.1V --->3.4Vkey   
           return KEY02;   
       else if(c>0x80)//0x200,2.5V --->2.91Vkey   
           return KEY03;   
       else if(c>0x66)//0x199,2.0V --->2.38Vkey   
           return KEY04;   
       else if(c>0x50)//0x11E,1.4V --->1.83Vkey    0x47   
           return KEY05;   
       else if(c>0x3A)//0x0EB,1.15V --->1.34Vkey 0x3A   
           return KEY06;   
       else if(c>0x2B)//0x0AE,0.85V --->1.03Vkey   
           return KEY07;   
       else if(c>0x19)//0x066,0.5V --->0.69Vkey   
           return KEY08;   
       else if(c>0x0A)//0x029,0.2V --->0.39Vkey   
           return KEY09;   
       else if(c>0x04)//0x012,0.09V --->0.18Vkey   
           return KEY0A;   
       else           //0x2E0,0V --->0Vkey   
           return KEY0B;   
    }   
       
    #pragma vector=ADC_vect   
    __interrupt void Adc_Interrupt(void)// two chanel Test OK!   
    {     
       
       KeyCurrent=Key_Judge(ADCH);   
       KeyCurrent|=(AUX_CHVal<<7);//Audge AD0 or AD1   
       if(KeyCurrent==NOKEY)   
        {   
           if(KeyCount>KEYCOUNT1)//key press and up    
            {   
              KeySuccessFlagSet;   
              KeyLongFlagClr;   
              //KeyPrevious=KeyCurrent;   
              KeyCount=0;   
              Send_Char(KeyPrevious);   
              return;   
             }   
           else// no key ，2 chanel 交替扫描   
            {   
              KeySuccessFlagClr;   
              KeyLongFlagClr;   
              KeyPrevious=NOKEY;   
              KeyCount=0;   
              ADMUX&=0xF8;   
              if(AUX_CHVal)//1 AD1   
                 AUX_CHSetAD0;   
              else   
                 AUX_CHSetAD1;   
              ADMUX|=AUX_CHVal;   
             }   
         }   
       else   
       {   
          if(KeyCurrent!=KeyPrevious)//与上次相同   
            {   
              KeyPrevious=KeyCurrent;   
              KeyCount=1;   
              KeyLongFlagClr;   
             }   
          else   
             KeyCount++;   
             
          if((KeyCount>KEYCOUNT2)||//达到长按次数   
             ((KeyCount>KEYCOUNT3)&&KeyLongFlagVal))//已经是长按键而且准备下一次操作   
           {   
              KeyPrevious=KeyCurrent;   
              KeyCount=1;   
              KeyLongFlagSet;   
              KeySuccessFlagSet;   
              Send_Char(KeyPrevious);   
              return;   
            }   
        }   
       ADC_Start;   
    }   
       
       
    void main( void )   
    {   
      PowerOn_Init();   
      MemoryInit_Check();   
      IEBus_Init();    
      RTC_Init();   
      SysOff_Init();   
      Send_Char(0x12);   
         
      EA_Enable;   
      ADC_Start;   
      ClrBit(PORTD,5);   
      LED2_Low;   
      LED1_Low;   
      RGBSw_High;   
      OSDSw_High;   
         
      while(1)   
      {     
           
        RTCCount++;   
        IEBusDataCheck();        
        if(IEBusRxFlagVal)//if IEBus receive ok ,process   
         {   
           EA_Disable;   
           Cmd_Process();   
           IEBusRxFlagClr;   
           EA_Enable;   
          }    
        if(KeySuccessFlagVal)//if key press   
         {    
           Key_Process();//send key to IEBus   
           KeySuccessFlagClr;   
           ADC_Start;     
          }   
        if(RedSuccess==1)   
         {          
           Remo_Process();   
           RedSuccess=0;          
           RedLong=0;      
           RedCmd=0xFF;   
           INT0_Enable;   
          }   
        }     
    }   